package com.yuke.cloud.service.omc.service.impl;

import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoExDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.omc.dao.OdsTeamSpecMapper;
import com.yuke.cloud.service.omc.entity.OdsTeamSpec;
import com.yuke.cloud.service.omc.service.OdsTeamSpecService;
import com.yuke.cloud.common.core.support.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/10/05.
 */
@Service
@Transactional
public class OdsTeamSpecServiceImpl extends BaseService<OdsTeamSpec> implements OdsTeamSpecService {
    @Resource
    private OdsTeamSpecMapper odsTeamSpecMapper;
    @Resource
    private WmcStorageFeignApi wmcStorageFeignApi;

    @Override
    public List<ProdSpecInfoExDto> getExtrInfo(List<OdsTeamSpec> list){
        List<Long> storages = list.stream().map(OdsTeamSpec::getStorageId).collect(Collectors.toList());
        Byte teamType = list.get(0).getTeamType();
        List<ProdSpecInfoExDto> storageList = wmcStorageFeignApi.getStorageSpecByStorageIds(storages,teamType);
        if (storageList == null || storageList.size() < 1){
            return storageList;
        }

        for (OdsTeamSpec odsTeamSpec : list){
            for (ProdSpecInfoExDto prod : storageList){
                if (odsTeamSpec.getStorageId().equals(prod.getStorageId())){
                   //如果拼单价格》0，使用拼单价格
                    if (odsTeamSpec.getSpecPrice() > 0){
                        prod.setSalePrice(odsTeamSpec.getSpecPrice());
                    }
                    //如果拼单图片存在，使用拼单图片
                    if (odsTeamSpec.getPicUrl().length() > 5){
                        prod.setBigPicture(odsTeamSpec.getPicUrl());
                    }
                    //如果拼单库存存在，使用拼单库存
                    if (odsTeamSpec.getSpecNum() > 0 ){
                        Integer num = odsTeamSpec.getSpecNum() - odsTeamSpec.getSaleNum();
                        if (num > 0){
                            prod.setStockCount(num);
                        }
                    }
                }
            }
        }
        return storageList;
    }
}
